/**
 * 
 */
package programs;

import global.*;
import heap.HFBufMgrException;
import heap.HFDiskMgrException;
import heap.HFException;
import heap.Heapfile;
import heap.InvalidSlotNumberException;
import heap.InvalidTupleSizeException;
import heap.InvalidTypeException;
import heap.Scan;
import heap.Tuple;
import iterator.FileScan;
import iterator.FldSpec;
import iterator.RelSpec;
import iterator.Sort;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import columnar.Columnarfile;
import columnar.TupleScan;

/**
 * @author usr
 *
 */
public class ReadCFExample {

	/**
	 * @param args
	 * @throws Exception 
	 */

	static String columnDBName = "ColumnDB2";

	static String columnarFileName = "ColumnarFile1";
	
	public static void main(String[] args) throws Exception {
			
        global.SystemDefs sysdef = new global.SystemDefs(columnDBName,0,300,"Clock");
        
        //Test1();
        //Test2();
        //Test3();
        //Test4();
        Test5();
        
       sysdef.JavabaseBM.flushAllPages();
	}
	
	// Scan and print all Tuples in the CF file
	static private void Test1() throws HFException, HFBufMgrException, HFDiskMgrException, IOException, Exception
	{
		diskmgr.PCounter.initialize();
		Columnarfile cf = new Columnarfile(columnarFileName);
		
		int numOfRcords = cf.getTupleCnt();
		System.out.println("# recs in ColFile:"+numOfRcords);
		
		TupleScan tscan = cf.openTupleScan();
		
		TID tid = new TID(cf.NumberOfColumns());
	
		for(int i = 0; i<numOfRcords;i++)
		{
			Tuple tuple = tscan.getNext(tid);
		//	System.out.println(tuple.getIntFld(1) + " " + tuple.getStrFld(2) + " " + tuple.getStrFld(3));
		}
		
		tscan.closeTuplescan();
		
		diskmgr.PCounter.print();
	}
	
	
	//Scan Single column
	static private void Test2() throws HFException, HFBufMgrException, HFDiskMgrException, IOException, Exception
	{
		diskmgr.PCounter.initialize();
		Columnarfile cf = new Columnarfile(columnarFileName);
		
		int numOfRcords = 998;//cf.getTupleCnt();
		System.out.println("# recs in ColFile:"+numOfRcords);
		
		Scan scan = cf.openColumnScan(2);

		RID rid = new RID();
        
        Tuple tuple = null;
	
		for(int i = 0; i<numOfRcords;i++)
		{
			tuple = scan.getNext(rid);
			Columnarfile.SetOneColumnTupleHeader(tuple, new AttrType(AttrType.attrString));
			System.out.println(tuple.getStrFld(1));
		}
		scan.closescan();
		
		diskmgr.PCounter.print();
	}
	
	//test delete tuple
	static private void Test3() throws HFException, HFBufMgrException, HFDiskMgrException, IOException, Exception
	{
		diskmgr.PCounter.initialize();
		Columnarfile cf = new Columnarfile(columnarFileName);

		int numOfRcords = cf.getTupleCnt();
		System.out.println("# recs in ColFile:"+numOfRcords);

		Scan scan = cf.openColumnScan(0);

		RID rid = new RID();

        Tuple tuple = null;

		for(int i = 0; i<100;i++)
		{
			tuple = scan.getNext(rid);
			Columnarfile.SetOneColumnTupleHeader(tuple, new AttrType(AttrType.attrString));
			//System.out.println(tuple.getStrFld(1)+"");
                        
		}
                scan.closescan();
               // cf.deleteTuple(tuple);

                System.out.println(cf.getTupleCnt());
		Test1();
		diskmgr.PCounter.print();
	}


	static private void Test4() throws Exception
	{
		diskmgr.PCounter.initialize();
		
		Columnarfile cf = new Columnarfile(columnarFileName);
        Tuple tuple = cf.getCompleteTupleByPosition(1028);
        tuple.print(cf.getAttrTypes());
        
    	diskmgr.PCounter.print();
	}

	
	static private void Test5() throws Exception
	{ 
	    TupleOrder[] order = new TupleOrder[2];
	    order[0] = new TupleOrder(TupleOrder.Ascending);
	    order[1] = new TupleOrder(TupleOrder.Descending);
	    short[] attrSize = new short[1];
	    attrSize[0] = 160;
	    AttrType[] attrType = new AttrType[1];
	    attrType[0] = new AttrType(AttrType.attrString);
		// create an iterator by open a file scan
	    FldSpec[] projlist = new FldSpec[1];
	    RelSpec rel = new RelSpec(RelSpec.outer); 
	    projlist[0] = new FldSpec(rel, 1);
	    
	    FileScan fscan = null;
	    
	    try {
	      fscan = new FileScan("ColumnarFile1.0", attrType, attrSize, (short) 1, 1, projlist, null);
	    }
	    catch (Exception e) {
	    		System.out.println("Err 1");
	    }
	     
	    // Sort "test2.in"
	    int count = 0;
	    Sort sort = null;
	    try {
	      sort = new Sort(attrType, (short) 1, attrSize, fscan, 1, order[1], (short)160, 100);
	    }
	    catch (Exception e) {
    		System.out.println("Err 2");
	    }

	    Tuple t = null;
	    
	  
        Tuple tuple = new Tuple();
        while ((tuple = sort.get_next()) != null) {
          tuple.print(attrType);
          count++;
        }
        
       
        System.out.println(count);
        sort.close();
	    
    


	}
	

}
